<!--采购计划-->
<template>
  <div class="app-container">
    <el-form :model="selectForm" ref="selectForm" :inline="true">
      <el-form-item label="流程KEY">
        <el-input v-model="selectForm.key" placeholder="请输入流程KEY" clearable size="small"/>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="selectForm.name" placeholder="请输入名称" clearable size="small"/>
      </el-form-item>
      <el-form-item label="所属分类">
        <el-input v-model="selectForm.category" placeholder="请输入所属分类" clearable size="small"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini"
                   @click="selectForm.pageNum=1;getList()">搜索
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-upload
            :action="baseUrl()+'/definition/upload'"
            :on-success="fileSuccess"
            :show-file-list="false"
            :before-upload="beforeAvatarUpload"
            :headers="headers"
            name="processDefinition"
            >
          <el-button size="mini" type="success" icon="el-icon-upload2">部署流程定义</el-button>
        </el-upload>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="datalist" border>
      <el-table-column align="center" type="index" label="序号" width="55"/>
      <el-table-column align="center" label="流程ID" property="id"/>
      <el-table-column align="center" label="流程KEY" property="key"/>
      <el-table-column align="center" label="流程名称" property="name"/>
      <el-table-column align="center" label="版本" property="version"/>
      <el-table-column align="center" label="流程描述" property="description"/>
      <el-table-column align="center" label="所属分类" property="category"/>
      <el-table-column align="center" label="部署时间" property="deploymentTime" width="170px">
        <template slot-scope="scope">
           <span>{{formatDate(scope.row.deploymentTime)}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="流程定义" property="resourceName" width="200px">
        <template slot-scope="scope">
          <a style="color: #2196F3;" :href="baseUrl()+'/definition/readResource?pdid='+scope.row.id+ '&resourceName='+scope.row.resourceName" target="_blank">{{scope.row.resourceName}}</a>
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="流程图" property="diagramResourceName">
        <template slot-scope="scope">
          <span style="color: #2196F3;cursor: pointer;" @click="info=scope.row;dialogVisible=true">{{scope.row.key}}.png</span>
          <!-- <img :src="baseUrl()+'/definition/readResource?pdid='+scope.row.id+ '&resourceName='+scope.row.diagramResourceName"> -->
        </template>
      </el-table-column>
      <el-table-column align="center" label="流程定义状态" property="suspendStateName">
        <template slot-scope="scope">
          <span class="badge badge-primary">{{scope.row.suspendStateName}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="280" >
        <template slot-scope="scope">
          <el-button v-show="scope.row.suspendState == 2" size="mini" type="info" icon="el-icon-check
" v-hasPermi="['purchaseGL:cgju:edit']" @click="suspendOrActiveApply(scope.row)">激活</el-button>
          <el-button size="mini" type="info" icon="el-icon-close" v-hasPermi="['purchaseGL:cgju:edit']" @click="suspendOrActiveApply(scope.row)">挂起</el-button>
          <el-button size="mini" type="warning" icon="el-icon-sort" v-hasPermi="['purchaseGL:cgju:edit']" @click="convert2Model(scope.row)">转型</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" v-hasPermi="['purchaseGL:cgju:delete']" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="selectForm.pageNum"
      :limit.sync="selectForm.pageSize"
      @pagination="getList"
    />
      <el-dialog
      title="查看流程图"
      :visible.sync="dialogVisible"
      class="look"
      width="1100px">
      <el-image v-if="info" :src="baseUrl()+'/definition/readResource?pdid='+info.id+ '&resourceName='+info.diagramResourceName"></el-image>
    </el-dialog>
  </div>
</template>
<script>
  import { getToken } from "@/utils/auth"
  import {formatDate} from "@/utils/index"
  import { definitionList, definitionSuspend ,definitionConvert,definitionDel} from '@/api/techProcess/index'
  export default {
    name: 'cgju',
    data() {
      return {
        headers: { Authorization: "Bearer " + getToken()},
        total:0,
        loading: false,
        datalist: [
          {anticipatedTotalNumber:'测试'}
        ],
        selectForm: {
          pageNum: 1,
          pageSize: 10,
          
        },
        date: '',
        myReview: false,
        dialogVisible:false,
        info:null
      }
    },
    created() {
     this.getList()
    },
    methods: {
      /** 查询列表 */
      async getList() {
        this.loading = true
        let formData = new FormData();
        for(var key in this.selectForm){
          formData.append(key,this.selectForm[key]);
        }
        definitionList(formData).then(response => {
          if(response.code==200){
            this.loading = false
            this.datalist = response.rows
            this.total = response.total
          }
        });
      },
      suspendOrActiveApply(row){
        //激活,挂起
        var suspendOrActive = row.suspendState == 2 ? '激活' : '挂起'
        this.$confirm('确认要'+ suspendOrActive + '流程定义吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var formdata = new FormData()
          formdata.append('id',row.id)
          formdata.append('suspendState',row.suspendState)
          definitionSuspend(formdata).then((res)=>{
            if(res.code==200){
              this.$message.success(res.msg)
              this.getList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      convert2Model(row) {
        //转型
        this.$confirm('确认要转换成流程模型吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var formdata = new FormData()
          formdata.append('processDefinitionId',row.id)
          definitionConvert(formdata).then((res)=>{
            if(res.code==200){
              this.$message.success(res.msg)
              this.getList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      dateSelect() {
        this.selectForm.applyBeginTime = this.date[0]
        this.selectForm.applyEndTime = this.date[1]
      },
      /** 删除*/
      del(row) {
        this.$confirm('确定删除该条流程定义信息吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          
          var formdata = new FormData()
          formdata.append('ids',row.deploymentId)

          definitionDel(formdata).then((res)=>{
            if(res.code==200){
              this.$message.success(res.msg)
              this.getList()
            }
          })
        }).catch(() => {
          this.$message.info('取消删除')
        })
      },
      beforeAvatarUpload(file){
        const isJPG =  /.bpmn|.zip|.bar/.test(file.name)
        if (!isJPG) {
          this.$message.error('流程定义文件仅支持 bpmn, zip 和 bar 格式！')
        }
        return isJPG
      },
      fileSuccess(res){
        if(res.code==200){
          this.$message.success(res.msg)
          this.getList()
        }
      },
      //时间转换
      formatDate,
      //部署
      bushu:function(){
        this.$confirm('确认要部署至流程定义吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          this.$message.success('部署成功')
        }).catch(() => {
          this.$message.info('取消部署')
        })
      },
    }
  }
</script>
<style scoped>
.bule {
    color: #2196F3;
    cursor: pointer;
    z-index: 100;
}
.look >>> .el-dialog__body{
  text-align: center;
  padding: 80px 0;
}
.look >>> .el-image{
  max-width: 950px;
}
</style>
